

var PATH_MAIN = Components.classes['@mozilla.org/file/directory_service;1'].getService(
	Components.interfaces.nsIProperties).get("ProfD", Components.interfaces.nsILocalFile).path;

var PATH_CONTENT = PATH_MAIN + "/extensions/firefox@nepomuk.org/chrome/content/";

var PATH_SCRIPTS = PATH_CONTENT + "scripts/";

var CURRENT_URL = window.opener.getBrowser().mCurrentBrowser.currentURI.spec;



function newLocalFile(fileName) {
	
	// create an nsILocalFile for the executable
	var file = Components.classes["@mozilla.org/file/local;1"]
								.createInstance(Components.interfaces.nsILocalFile);
	file.initWithPath(fileName);
	return file;
	
}


function removeLocalFile(fileName) {
	
	var file = newLocalFile(fileName);
	if (file.exists()) {
		file.remove(false); // false means "non-recursive". As we remove a file here, no need.
	}
	
}


function run (blocking, args) {
	
	//alert(args);
	
	file = newLocalFile("/usr/bin/python");
	
	// create an nsIProcess
	var process = Components.classes["@mozilla.org/process/util;1"]
									.createInstance(Components.interfaces.nsIProcess);
	process.init(file);
	
	// Run the process.
	// If first param is true, calling thread will be blocked until
	// called process terminates.
	// Second and third params are used to pass command-line arguments
	// to the process.
	
	var scriptPath = PATH_SCRIPTS + "python_calls.py";
	
	// add to arguments to the ones given by the user: the fileName of the script (python_calls.py) and the results path to write the results files in (here = PATH_CONTENT)
	args2 = [scriptPath, PATH_CONTENT];
	for (var i = 0 ; i < args.length ; i++) {
		args2.push(args[i]);
	}
	
	process.run(blocking, args2, args2.length);
	
}


function getExistingFileContent (file) {
	
	//script code found on web page http://www.captain.at/programming/xul/ (available on the 2008-06-27)
	
	try {
		//TODO: remove that?
		netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
	} catch (e) {
		alert("Permission to read file was denied.");
	}
	
	var is = Components.classes["@mozilla.org/network/file-input-stream;1"]
		.createInstance( Components.interfaces.nsIFileInputStream );
	is.init(file, 0x01, 00004, null);
	var sis = Components.classes["@mozilla.org/scriptableinputstream;1"]
		.createInstance(Components.interfaces.nsIScriptableInputStream );
	sis.init(is);
	
	var output = sis.read(sis.available());
	return output;
	
}


function getFileContent (fileName) {
	
	var file = newLocalFile(fileName);
	
	if (file.exists()) {
		//alert(fileName+" exists");
		return getExistingFileContent(file);
	} else {
		//alert(fileName+" does not exist");
		return "";
	}
	
}


function success (inputFile, noContentMessage) {
	
	//alert("success("+inputFile+", "+noContentMessage+")");
	
	var error = getFileContent(PATH_CONTENT + "error.log");
	removeLocalFile(PATH_CONTENT + "error.log");
	
	if (error != "") {
		
		alert(error);
		return null;
		
	} else {
		
		if (inputFile != null) {
			
			// Get the (probably) existing content of the result file
			var content = getFileContent(inputFile);
			removeLocalFile(inputFile);
			
			if (content != "") {
				
				if (content == "None") {
					return noContentMessage;
				} else {
					return content;
				}
				
			} else {
			
				alert("An unknown error has occurred");
				return null;
				
			}
			
		} else {
			
			// There is no result file to check, so we suppose everything worked fine.
			return "No file to retrieve content from."
			
		}
		
	}
	
}




